package com.moss.web;

import com.moss.common.core.BaseController;
import com.moss.common.domain.AjaxResult;
import com.moss.common.domain.SysLogin;
import com.moss.common.entity.SysMenuEntity;
import com.moss.common.entity.SysUserEntity;
import com.moss.system.service.MenuService;
import com.moss.system.service.LoginService;
import com.moss.system.service.PermissionService;
import com.moss.system.service.UserService;
import com.moss.common.vo.menu.RouterVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author gxcc3
 * @date: 2024/8/22 23:11
 */
@RestController
public class LoginController extends BaseController {

    @Resource
    LoginService loginService;

    @Resource
    MenuService menuService;

    @Resource
    UserService userService;

    @Resource
    PermissionService permissionService;


    @PostMapping("/login")
    public AjaxResult<SysLogin> login(@RequestBody SysUserEntity userLogin){
        SysLogin login = loginService.login(userLogin);
        return AjaxResult.success(login);
    }

    @GetMapping("/getUserInfo")
    public AjaxResult<Map<String,Object>> getUserInfo(){
        SysUserEntity user = userService.getUserInfo(getUserId());
        Set<String> roles = permissionService.getRolePermissionList(user);
        Set<String> permissions = permissionService.getMenuPermissionList(user);

        Map<String,Object> result = new HashMap<>(2);
        result.put("user", user);
        result.put("roles", roles);
        result.put("permissions", permissions);
        return AjaxResult.success(result);
    }

    @GetMapping("/getRouters")
    public AjaxResult<List<RouterVO>> getRouters(){
        List<SysMenuEntity> menus = menuService.selectMenuListByUserId(getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    @PostMapping("logout")
    public AjaxResult<Void> logout(){
        return AjaxResult.success();
    }
}
